var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();

Page({
  data: {
    userInfo: {},
    Length: 6, //输入框个数    
    isFocus: true, //聚焦    
    Value: "", //输入的内容
    ispassword: false, //是否密文显示 true为密文， false为明文。    
    disabled: true
  },
  Focus(e) {
    var that = this;
    var inputValue = e.detail.value;
    var ilen = inputValue.length;
    if (ilen == 6) {
      that.setData({
        disabled: false,
      })
    } else {
      that.setData({
        disabled: true,
      })
    }
    that.setData({
      Value: inputValue,
    })
  },
  Tap() {
    var that = this;
    that.setData({
      isFocus: true,
    })
  },
  formSubmit(e) {
    console.log(e.detail.value.password);
  },
  onLoad: function(options) {

  },
  onShow: function() {

  },
  bindGetUserInfo(e) {
    let that = this;

    wx.showLoading({
      title: '验证中...',
    });

    if (!wx.getStorageSync('userInfo')){
      wx.login({
        success: function (res) {
          if (res.code) {
            //登录远程服务器
            util.request(api.AuthLoginByWeixin, {
              code: res.code,
              userInfo: e.detail
            }, 'POST', 'application/json').then(res => {
              if (res.errno === 0) {  //登录远程服务器的errno判断
                //存储用户信息
                app.globalData.userInfo = res.data.userInfo;
                app.globalData.userId = res.data.userId;
                app.globalData.token = res.data.token;
                app.globalData.openId = res.data.openId;
                wx.setStorageSync('userInfo', res.data.userInfo);
                wx.setStorageSync('token', res.data.token);
                wx.setStorageSync('userId', res.data.userId);
                wx.setStorageSync('openId', res.data.openId);

                //检查邀请码是否输入正确
                that.checkinvitecurrent(res.data.userId);
              } else {
                wx.hideLoading();
                wx.showModal({
                  title: '提示',
                  content: res.errmsg,
                  showCancel: false
                });
              }
            });
          }
        }
      });
    }
    else{
      //检查邀请码是否输入正确
      that.checkinvitecurrent(wx.getStorageSync('userId'));
    }
  },
  //检查邀请码是否输入正确
  checkinvitecurrent: function (useid) {
    let that = this;
    util.request(api.Checkinvitecurrent, {
      userId: useid,
      invitation: that.data.Value,
    }).then(res => {
      if (res.errno === 0) {  //检查有无输入过邀请码的errno判断
        //获取商家列表
        that.getShops();
      } else {
        wx.navigateTo({
          url: '/pages/login/btnAuth/btnAuth',
        });
        wx.showModal({
          title: '提示',
          content: res.errmsg,
          showCancel: false
        });
      }
    });
  },
  //获取所属店铺列表  
  getShops: function () {
    let that = this;
    util.request(api.GetMyShops).then(function (res) {
      //计算店铺总数
      let myShopCount = 0;
      let collectedShopCount = 0;

      let myShops = res.data.myShops;
      if (myShops && myShops.length > 0) {
        //是店主
        app.globalData.isShopKeeper = true;
        wx.setStorageSync('isShopKeeper', true);
        wx.setStorageSync('myShops', myShops);
        myShopCount += myShops.length;
      }
      else {
        //不是店主
        app.globalData.isShopKeeper = false;
        wx.setStorageSync('isShopKeeper', false);
      }

      //获取所属店铺列表
      util.request(api.GetCollectedShops).then(function (res) {
        //收藏的店铺列表
        let collectedShops = res.data.collectedShops;

        if (collectedShops) {
          collectedShopCount += collectedShops.length;
        }

        //当有收藏的店铺或者自己的店铺时
        if (myShopCount + collectedShopCount > 0) {
          //只有收藏店铺列表大于1并且没有自己的店铺时
          if (collectedShopCount == 1 && myShopCount == 0) {
            wx.setStorageSync('collectedShops', collectedShops);
            wx.setStorageSync('currentShop', collectedShops[0]);
            app.globalData.currentRootDeptId = collectedShops[0].rootDeptId;
            app.globalData.currentUserGrade = collectedShops[0].userGrade;

            //直接跳转至该店铺首页
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
          else {
            //跳转至店铺列表页
            wx.setStorageSync('collectedShops', collectedShops);
            wx.navigateTo({
              url: '/pages/shopIndex/shopIndex',
            })
          }
        }
      })
    })
  },
})